"""
作者:赵威
继承Base类
后太添加新商品的页面

"""
from time import sleep

from Common.Base import open_browser
from Page.BackStageLoginPage import BackStageLoginPage

login_url = "http://172.16.7.121:8080/ecshop/admin/index.php"


class AddGoodsPage(BackStageLoginPage):
    """制作定位器"""
    loc_goods_menu2 = ('xpath', '//*[@id="menu-ul"]/li[2]')  # 商品管理菜单按钮
    loc_add_goods_menu = ('xpath', '//*[@id="sub-menu-02_goods_add"]/a')  # 添加新商品按钮
    loc_goods_name = ('name', 'goods_name')  # 商品名称输入框1
    loc_goods_name_color = ('id', 'font_color')  # 字体颜色选框2
    loc_goods_name_style = ('name', 'goods_name_style')  # 字体样式选框3
    loc_goods_num0 = ('name', 'goods_sn')  # 商品货号定位4
    loc_goods_class = ('name', 'cat_id')  # 商品分类的下拉框5
    loc_classification = ('name', 'other_cat[]')  # 拓展分类下拉框6
    loc_goods_brand = ('name', 'brand_id')  # 商品品牌下拉框7
    loc_select_supplier = ('name', 'suppliers_id')  # 选择供应商下拉框8
    loc_sell_price = ('name', 'shop_price')  # 本店售价输入框9
    loc_vip_price1 = ('id', 'rank_1')  # 会员价格注册用户输入框10
    loc_vip_price2 = ('id', 'rank_3')  # 会员价格代销用户输入框11
    loc_vip_price3 = ('id', 'rank_2')  # 会员价格vip输入框12
    loc_Preferential_quantity = ('name', 'volume_number[]')  # 优惠数量输入框13
    loc_Preferential_price = ('name', 'volume_price[]')  # 优惠价格输入框14
    loc_market_price = ('name', 'market_price')  # 市场价格输入框15
    loc_virtual_sales = ('name', 'virtual_sales')  # 虚拟销量输入框16
    loc_give_integral = ('name', 'give_integral')  # 赠送消费积分17
    loc_rank_integral = ('name', 'rank_integral')  # 赠送等级积分数输入框18
    loc_integral = ('name', 'integral')  # 积分购买金额输入框19
    loc_is_promote = ('id', 'is_promote')  # 促销价的选择框20
    loc_promote_1 = ('id', 'promote_1')  # 促销价格输入框21
    loc_time_promote1 = ('xpath', '//*[@id="selbtn1"]/img')  # 促销日期的日历弹窗22
    loc_time_promote2 = ('xpath', '//*[@id="selbtn2"]/img')  # 促销日期的日历弹窗23
    loc_goods_img = ('name', 'goods_img')  # 上传商品图片文件的链接按钮24
    loc_goods_img2 = ('xpath','//*[@id="general-table"]/tbody/tr[17]/td[2]/input[2]')
    loc_goods_easy_img = ('id', 'auto_thumb')  # 自动生成缩略图功能键25
    loc_select_ture = ('xpath', '//*[@id="tabbody-div"]/form/div/input[2]')  # 页面的确定按钮26
    loc_reset = ('xpath', '//*[@id="tabbody-div"]/form/div/input[3]')  # 页面的重置按钮27
    loc_describe = ('id', 'detail-tab')  # 详细描述的页面定位28
    loc_input_text = ('xpath', '/html/body')  # 详细描述文本的输入29
    loc_describe_ture = ('xpath', '//*[@id="tabbody-div"]/form/div/input[2]')  # 详细描述的确定按钮30
    loc_describe_reset = ('xpath', '//*[@id="tabbody-div"]/form/div/input[3]')  # 定位详细描述的确定按钮31
    loc_main_iframe = ('main-frame')  # main_iframe32
    loc_goods_desc___Frame = ('id', 'goods_desc___Frame')  # main_iframe下的第二层iframe33
    loc_text_iframe = ('xpath', '//*[@id="xEditingArea"]/iframe')  # 文本输入框的iframe34
    loc_main_menu_farame = 'menu-frame'  # 主菜单的iframe35
    loc_goods_weight = ('name', 'goods_weight')  # 商品重量输入框36
    loc_goods_weight_unit = ('name', 'weight_unit')  # 商品重量的单位37
    loc_goods_number = ('xpath', '//*[@id="mix-table"]/tbody/tr[2]/td[2]/input')  # 商品库存的输入框38
    loc_warn_number = ('name', 'warn_number')  # 库存警告数量39
    loc_get_text_value = ('css selector','tr:nth-child(3)>td.first-cell >span')  # 新增商品的名称定位
    loc_other = ('id','mix-tab')  # 其他信息的点击框
    """封装操作层,操作元素"""

    def click_main_menu(self):
        """点击商品管理"""
        self.join_iframe('menu-frame')
        self.click(self.loc_goods_menu2)
        self.quit_iframe()

    def click_other(self):
        """点击其他信息"""
        self.join_iframe('main-frame')
        self.click(self.loc_other)
        self.quit_iframe()

    def input_goods_number(self,num):
        self.join_iframe('main-frame')
        self.send_keys(num,self.loc_goods_number)
        self.quit_iframe()

    def click_add_menu(self):
        """点击新增商品"""
        self.join_iframe('menu-frame')
        self.click(self.loc_add_goods_menu)
        self.quit_iframe()

    def input_goods_name(self, goodsname):
        """输入商品名称"""
        self.join_iframe('main-frame')
        self.send_keys(goodsname, self.loc_goods_name)
        self.quit_iframe()

    def click_word_style(self):
        """点击字体样式下拉菜单"""
        self.join_iframe('main-frame')
        self.click(self.loc_goods_name_style)
        self.quit_iframe()

    def input_goods_num(self, text):
        """输入商品货号"""
        self.join_iframe('main-frame')
        self.send_keys(text, self.loc_goods_num)
        self.quit_iframe()

    def click_goods_class(self, index: int):
        """点击商品分类下拉菜单"""
        self.join_iframe('main-frame')
        self.select_drop_down_menu(index=index,locator=self.loc_goods_class)
        self.quit_iframe()

    def click_classification(self, index: int):
        """点击拓展分类下拉菜单"""
        self.join_iframe('main-frame')
        self.select_drop_down_menu(index=index, locator=self.loc_classification)
        self.quit_iframe()

    def click_goods_brand(self, index: int):
        """品牌下拉框"""
        self.join_iframe('main-frame')
        self.select_drop_down_menu(index=index, locator=self.loc_goods_brand)
        self.quit_iframe()

    def click_supplier(self, index: int):
        """供应商下拉框"""
        self.join_iframe('main-frame')
        self.select_drop_down_menu(index=index, locator=self.loc_goods_brand)
        self.quit_iframe()

    def input_sell_price(self, text):
        """本店售价输入框"""
        self.join_iframe('main-frame')
        self.send_keys(text, locator=self.loc_sell_price)
        self.quit_iframe()


    def input_vip_price_submit(self, text):
        """会员价格注册用户输入框"""
        self.join_iframe('main-frame')
        self.send_keys(text, self.loc_vip_price1)
        self.quit_iframe()

    def input_vip_price_consignment(self, text):
        """会员价格代销用户输入"""
        self.join_iframe('main-frame')
        self.send_keys(text, self.loc_vip_price2)
        self.quit_iframe()

    def input_vip_price(self, text):
        """会员价格代销用户输入"""
        self.join_iframe('main-frame')
        self.send_keys(text, self.loc_vip_price3)
        self.quit_iframe()

    def input_preferential_quantity(self, text):
        """优惠数量输入框"""
        self.join_iframe('main-frame')
        self.send_keys(text, self.loc_Preferential_quantity)
        self.quit_iframe()

    def input_preferential_price(self, text):
        """商品价格输入框"""
        self.join_iframe('main-frame')
        self.send_keys(text, self.loc_Preferential_price)
        self.quit_iframe()

    def input_market_price(self, text):
        """商品价格输入框"""
        self.join_iframe('main-frame')
        self.send_keys(text, self.loc_market_price)
        self.quit_iframe()

    def input_virtual_sales(self, text):
        """虚拟销量输入框"""
        self.join_iframe('main-frame')
        self.send_keys(text, self.loc_market_price)
        self.quit_iframe()

    def input_give_integral(self, text):
        """赠送消费积分输入框"""
        self.join_iframe('main-frame')
        self.send_keys(text, self.loc_give_integral)
        self.quit_iframe()

    def input_rank_integral(self, text):
        """赠送等级积分输入框"""
        self.join_iframe('main-frame')
        self.send_keys(text, self.loc_rank_integral)
        self.quit_iframe()

    def input_integral(self, text):
        """赠送积分购买输入框"""
        self.join_iframe('main-frame')
        self.send_keys(text, self.loc_integral)
        self.quit_iframe()

    def click_is_protome(self):
        """促销价选择框"""
        self.join_iframe('main-frame')
        self.click_radio(self.loc_is_promote)
        self.quit_iframe()

    def click_protome_1(self, text):
        """促销价输入框"""
        self.join_iframe('main-frame')
        self.send_keys(text, self.loc_promote_1)
        self.quit_iframe()

    def click_time_start_protome_1(self, text):
        """促销起始日期选择框"""
        self.join_iframe('main-frame')
        self.send_keys(text, self.loc_promote_1)
        self.quit_iframe()

    def click_time_end_protome_1(self, text):
        """促销截至日期选择框"""
        self.join_iframe('main-frame')
        self.send_keys(text, self.loc_time_promote2)
        self.quit_iframe()

    def click_goods_img(self):
        """点击图片上传的链接按钮"""
        self.join_iframe('main-frame')
        self.click(self.loc_goods_img)
        self.quit_iframe()

    def main_click_menu(self):
        """点击主菜单"""
        self.join_iframe('menu-frame')
        self.click(self.loc_goods_menu2)
        self.quit_iframe()

    def add_goods_menu(self):
        """点击新增商品"""
        self.join_iframe('menu-frame')
        self.click(self.loc_add_goods_menu)
        self.quit_iframe()

    def input_link_img(self,text):
        """输入图片链接"""
        self.join_iframe('main-frame')
        self.send_keys(text,self.loc_goods_img2)
        self.quit_iframe()

    def click_ture(self):
        """点击确定按钮"""
        self.join_iframe('main-frame')
        self.click(self.loc_select_ture)
        self.quit_iframe()

    def get_goods_text(self):
        """获取新增成功的商品名称"""
        self.join_iframe('main-frame')
        result = self.get_element_text(locator=self.loc_get_text_value)
        self.quit_iframe()
        return result

if __name__ == '__main__':
    driver = open_browser()  # 打开浏览器
    login = AddGoodsPage(driver)  # 实例化对象
    login.open_url(login_url)  # 输入网址
    login.remove_alerta_window(id='CMask')
    login.remove_alerta_window(id='panelCloud')
    # 0.点击ecshop登录按钮
    login.click_ecshop()

    # 1.输入用户名
    username = 'admin'
    login.input_username(username)
    # 2.输入密码
    password = 'admin123456'
    login.input_password(password)
    # 4.点击立即登录
    login.click_submit()
    # 点击主菜单
    login.click_main_menu()
    # 点击添加菜单按钮
    login.click_add_menu()

    # 点击通用信息
    login.input_goods_name('欧尼,故乡的菊花开了')

    # 选择下拉菜单
    login.click_goods_class(index=2)

    # 输入本店售价
    login.input_sell_price('500')

    # 上传图片
    login.input_link_img('https://img0.baidu.com/it/u=2352052024,4279709682&fm=11&fmt=auto&gp=0.jpg')

    # 点击确定
    login.click_ture()
    sleep(3)

    result = login.get_goods_text()
    print(result)